﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Text;
using ChanExt;
using C.Unite;
using C.Entities;
using MySoft.Data;

namespace ArtLand.OA.Project.SystemMgr
{
    public partial class SysUsers : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.OnClientClick = Window1.GetShowReference("SysUserChange.aspx", "新增管理员");
                BindGrid(GetWhere());
            }
        }

        #region BindGrid
        protected void T1_Click(object sender, EventArgs e)
        {

        }


        private void BindGrid(WhereClip wc)
        {
            Grid1.SelectedRowIndex = -1;
            // 1.设置总项数
            Grid1.RecordCount = TbsDbSession.Default.From<Sys_Users>().Where(wc).Count();

            List<Sys_Users> items = TbsDbSession.Default.From<Sys_Users>().Where(wc).OrderBy(Sys_Users._.UserID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (Sys_Users._.UserName.Contain(ttbxMyBox2.Text) || Sys_Users._.TrueName.Contain(ttbxMyBox2.Text));
            }
            return wc;
        }



        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
        }


        //protected void btnDelete_Click(object sender, EventArgs e)
        //{
        //    int returnvalue = 0;
        //    bool bl = true;
        //    foreach (int row in Grid1.SelectedRowIndexArray)
        //    {
        //        int id=int.Parse(Grid1.DataKeys[row][0].ToString());
        //        int c=TbsDbSession.Default.From<Sys_Modules>().Where(Sys_Modules._.Topmid==id).Count();
        //        if (c == 0)
        //        {
        //            returnvalue += TbsDbSession.Default.Delete<Sys_Modules>(Sys_Modules._.Mid == id);
        //        }
        //        else
        //        {
        //            bl = false;
        //        }

        //    }
        //    if (returnvalue > 0)
        //    {
        //        if (bl == false)
        //        {
        //            Alert.ShowInTop("删除成功！但有些模块还存在子模块！");
        //            BindGrid();
        //        }
        //        else
        //        {
        //            Alert.ShowInTop("删除成功！");
        //            BindGrid();
        //        }
        //    }

        //}


        #endregion
        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
        {
            int d = 0;
            Sys_UserGroup ug = TbsDbSession.Default.From<Sys_UserGroup>().Where(Sys_UserGroup._.Ugid == e.Values[2]).ToFirst();
            if (null != ug)
            {
                e.Values[2] = ug.Groupname;
            }
            else
            {
                e.Values[2] = "未知";
            }
        }

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<Sys_Users>(Sys_Users._.UserID == id);
                Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }
            if (e.CommandName == "CheckBox1")
            {
                CheckBoxField checkField = (CheckBoxField)Grid1.FindColumn(e.ColumnIndex);
                bool checkState = checkField.GetCheckedState(e.RowIndex);
                Sys_Users tea = new Sys_Users();
                tea.UserID = id;
                tea.Attach();
                tea.IsLock = checkState;
                TbsDbSession.Default.Update<Sys_Users>(tea);
                //TbsDbSession.Default.FromSql("update Sys_Users set IsLock='" + checkState + "' where id=" + id).ExecuteNonQuery();
                BindGrid(GetWhere());
            }
        }
    }
}